package com.bindatax.core.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DecimalFormat;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;

public class StringUtils {
    private final static AtomicLong idHelper = new AtomicLong();
    public static String percent(Number v1 , Number v2){
        float size = (float)v1.longValue()/v2.longValue();
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数，不足的补0
        return df.format(size);//返回的是String类型的
    }

    public static String getExceptionInfo(Throwable e){
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return sw.toString();
    }

    public static String generateUniqueId() {
        return "event_" + idHelper.incrementAndGet() + "_" + UUID.randomUUID().toString().replace("-", "");
    }

    public static String getValue(String value){
        return getValue(value,"");
    }
    public static String getValue(String value,String defaultValue){
        return value == null ? defaultValue : value;
    }

}
